package com.huaxonline.boot.doc;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springdoc.core.GroupedOpenApi;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;

/**
 * 文档自动配置类
 * 基于 SpringDoc(OpenAPI3) + Knife4j
 */
@Configuration
@ConditionalOnProperty(name = "huaxonline.doc.enabled", matchIfMissing = true)
public class DocAutoConfiguration implements BeanFactoryAware {
    private static final String AUTH_KEY = "Authorization";

    private BeanFactory beanFactory;

    @Bean
    @ConditionalOnMissingBean
    public DocProperties docProperties() {
        return new DocProperties();
    }

    @Bean
    @ConditionalOnMissingBean
    public GroupedOpenApi defaultApi(DocProperties props){
        String basePackage = props.getBasePackage();
        GroupedOpenApi.Builder builder = GroupedOpenApi.builder().group("default");
        if(basePackage != null && !basePackage.isEmpty()){
            builder.packagesToScan(basePackage);
        }
        return builder.build();
    }

    @Bean
    public OpenAPI openAPI(DocProperties props) {
        Info info = new Info()
                .title(props.getTitle())
                .description(props.getDescription())
                .version(props.getVersion());
        
        // 设置许可证信息（如果配置了）
        if (props.getLicense() != null && !props.getLicense().isEmpty()) {
            info.license(new io.swagger.v3.oas.models.info.License()
                    .name(props.getLicense())
                    .url(props.getLicenseUrl()));
        }
        
        // 设置联系人信息（如果配置了）
        if (props.getContact() != null && 
            (props.getContact().getName() != null && !props.getContact().getName().isEmpty() ||
             props.getContact().getEmail() != null && !props.getContact().getEmail().isEmpty() ||
             props.getContact().getUrl() != null && !props.getContact().getUrl().isEmpty())) {
            info.contact(new Contact()
                    .name(props.getContact().getName())
                    .url(props.getContact().getUrl())
                    .email(props.getContact().getEmail()));
        }

        SecurityScheme scheme = new SecurityScheme()
                .type(SecurityScheme.Type.APIKEY)
                .in(SecurityScheme.In.HEADER)
                .name(AUTH_KEY);

        return new OpenAPI()
                .info(info)
                .schemaRequirement(AUTH_KEY, scheme)
                .addSecurityItem(new SecurityRequirement().addList(AUTH_KEY));
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }
}